import useUserStore from '@/stores/user';
import { showDialog, showToast } from 'vant';
import { createRouter, createWebHistory } from 'vue-router';
import type {RouteLocationNormalized,NavigationGuardNext} from 'vue-router'
export const staticRoutes = [

  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/rank',
    name: 'Rank',
    component: () => import('@/views/rank/index.vue'),
  },
  {
    path: '/classification',
    name: 'Classification',
    component: () => import('@/views/classification/index.vue'),
  },
  {
    path: `/category2`,
    name: "Category2",
    component: () => import('@/views/category2/index.vue'),
  },
  {
    path: `/category3`,
    name: "Category3",
    component: () => import('@/views/category3/index.vue'),
  },
  {
    path: '/listenlist',
    name: 'Listenlist',
    component: () => import('@/views/listenlist/index.vue'),
  },
  {
    path: '/listenlist/iframe',
    component: () => import('@/views/listenlist/components/iframe/index.vue'),
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('@/views/detail/index.vue'),
  },
  {
    path: '/home',
    component: () => import('@/views/home/index.vue'),
  },

  {
    path: '/course/:albumId',
    name: 'Course',
    component: () => import('@/views/course/index.vue'),
  },
  {
    path: '/anchordeatil',
    name: 'Anchordeatil',
    component: () => import('@/views/anchordeatil/index.vue'),
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/search/index.vue'),
  },
  {
    path: '/searchdetail/:keyWord?',
    name: 'Searchdetail',
    component: () => import('@/views/searchdetail/index.vue'),
    beforeEnter: async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext):Promise<void>=> {
     if(to.path=='/searchdetail'){
         await showDialog({
          title:'不能从地址栏乱入哦！'
         });
         next('/home');
     }else{
      next();
     }
    },
  },
  {
    path: '/so',
    name: 'So',
    component: () => import('@/views/so/index.vue'),
  },
  {
    path: "/",
    redirect: "/home"
    
   
  }
];
const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes, // 不需要筛选的路由直接注册
  // 不能够直接把所有的路由写在这里注册,应该把筛选之后有权限的路由进行注册
  // routes: staticRoutes.concat(allAsyncRoutes, anyRoute),
  // 滚动行为
  scrollBehavior() {
    return { top: 0, left: 0 }
  },
})
//路由守卫
router.beforeEach(async (to,from,next)=>{
  const userStore=useUserStore()
  let token=userStore.token

  if(token){
     if(to.path.includes('/login')){

      showToast('你已经登录了哦');


      next('/home');
      return
     }
    let userInfo=userStore.userInfo
    if(userInfo){
      next()
    }else{
      //没有用户信息

      try {
        await userStore.getUserInfo()
        next()
      } catch (error) {
        //获取用户失败
        userStore.clearUserInfo()
        next('/login')
        
      }
    }
  }else{
    //没有登录
   
    if(to.path.includes('/searchdetail')){
      next(`/login?redirect=${to.path}`)
    }else{
      next()
    }
  }
})
// 导出路由
export default router;
